#include "blinkercfg.h"
#include "Blinker.h"
#include "disp.h"
#include "ui.h"
#include "sht.h"
#include "rtc.h"
#include "eeconfig.h"
#include "Blinker.h"
#include "wifi.h"

BlinkerSlider app_red_value("rvalue");
BlinkerSlider app_green_value("gvalue");
BlinkerSlider app_blue_value("bvalue");
BlinkerSlider app_bright_value("bright");

const char *device_key = "e381fbfca127";
const char *ssid     = "IceSu";
const char *password = "2ap99018";

void rtData(void)
{
    char str[10] = {0}; 
    sprintf(str, "#%.6X", getFontColor());
    Blinker.sendRtData("rvalue", (getFontColor()>>16)&0xFF);
    Blinker.sendRtData("gvalue", (getFontColor()>>8)&0xFF);
    Blinker.sendRtData("bvalue", getFontColor()&0xFF);
    Blinker.sendRtData("bright", getConfigBrightness());
    Blinker.sendRtData("Temp", sht31.temp/100);
    Blinker.sendRtData("Humi", sht31.humi/100);
    app_bright_value.color(String(str));
    app_bright_value.print(getConfigBrightness());
    Blinker.printRtData();
}

void redSliderCbk(int32_t value)
{
    uint32_t color = getFontColor();
    color &= ~(0xFF<<16);
    color |= (value<<16);
    setFontColor(color);
    saveColorConfig(color, getConfigBrightness());
}

void greenSliderCbk(int32_t value)
{
    uint32_t color = getFontColor();
    color &= ~(0xFF<<8);
    color |= (value<<8);
    setFontColor(color);
    saveColorConfig(color, getConfigBrightness());
}

void blueSliderCbk(int32_t value)
{
    uint32_t color = getFontColor();
    color &= ~(0xFF);
    color |= (value);
    setFontColor(color);
    saveColorConfig(color, getConfigBrightness());
}

void brightSliderCbk(int32_t value)
{
    setConfigBrightness(value);
    saveColorConfig(getFontColor(), value);
}

void heartbeatCbk(void)
{
    getNtpTime();
}

void miotCbk(int32_t queryCode)
{
    switch(queryCode)
    {
        case BLINKER_CMD_QUERY_TEMP_NUMBER:
            BlinkerMIOT.temp(sht31.temp/100);
            BlinkerMIOT.print();
        break;
        case BLINKER_CMD_QUERY_HUMI_NUMBER:
            BlinkerMIOT.humi(sht31.humi/100);
            BlinkerMIOT.print();
        break;
        default:
            BlinkerMIOT.temp(sht31.temp/100);
            BlinkerMIOT.humi(sht31.humi/100);
            BlinkerMIOT.print();
        break;
    }
}

void dataStorge(void)
{
    Blinker.dataStorage("TempChart", (float)sht31.temp/100.0);
    Blinker.dataStorage("HumiChart", (float)sht31.humi/100.0);
}

void blinkerInit(void)
{
    Blinker.begin(blinker_key, wifi_ssid, wifi_pwd);
    // BLINKER_DEBUG.stream(Serial);
    // BLINKER_DEBUG.debugAll();
    Blinker.attachRTData(rtData);
    Blinker.attachHeartbeat(heartbeatCbk);
    app_red_value.attach(redSliderCbk);
    app_blue_value.attach(blueSliderCbk);
    app_green_value.attach(greenSliderCbk);
    app_bright_value.attach(brightSliderCbk);
    Blinker.attachDataStorage(dataStorge);
    BlinkerMIOT.attachQuery(miotCbk);
    Blinker.setTimezone(8.0);
}

void blinkerRun(void)
{
    if(isVaildChar(blinker_key[0]))
        Blinker.run();
}

void blinkerConnect(void)
{
    Blinker.connect();
}

void blinkerDisconnect(void)
{
    Blinker.disconnect();
}

/*Get NTP time from blinker*/
void getNtpTime(void)
{
    if(isVaildChar(blinker_key[0]))
    {
        // Serial.println(timeClient.getFormattedTime());
        rtc.year = (byte)(Blinker.year() - 2000);
        rtc.month = (byte)Blinker.month();
        rtc.date = (byte)Blinker.mday();
        rtc.week = (byte)Blinker.wday();
        rtc.hour = (byte)Blinker.hour();
        rtc.min = (byte)Blinker.minute();
        rtc.sec = (byte)Blinker.second();
        time_set = 1;
    }
}
